/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pfc.arbol;

import java.awt.Component;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
import org.apache.commons.io.IOUtils;
import org.xml.sax.InputSource;
import pfc.arbol.DBTreeNode;
import pfc.arbol.ProyectoTreeNode;

/**
 *
 * @author xandru
 */
public class CellRenderer extends DefaultTreeCellRenderer {

    final String db_icon_path = "/resources/Database.gif";
    final String project_icon_path = "/resources/project.gif";
    ImageIcon dbIcon = null;
    ImageIcon projectIcon = null;
    ImageIcon intermediateIcon = null;
    String[] nodos = {  ProyectoTreeNode.class.getSimpleName(),
                        DBTreeNode.class.getSimpleName(),
                        EntityTreeNode.class.getSimpleName(),
                        RelationTreeNode.class.getSimpleName()
                    };
    String[] iconPath ={ "/resources/project.gif",
                         "/resources/Database.gif",
                         "/resources/table.gif",
                         "/resources/relationship.gif"
                    };
    HashMap<String, ImageIcon> iconMap = new HashMap<String, ImageIcon>();

    public CellRenderer() {
        super();

        //InputSource is_dbicon = new InputSource(getClass().getResourceAsStream(db_icon_path));
        //InputSource is_projectIcon = new InputSource(getClass().getResourceAsStream(project_icon_path));
        try {

            for (int i = 0; i < nodos.length; i++) {
                InputSource is = new InputSource(getClass().getResourceAsStream(iconPath[i]));
                ImageIcon ii = new ImageIcon(IOUtils.toByteArray(is.getByteStream()));
                iconMap.put(nodos[i], ii);
            }
            //dbIcon = new ImageIcon(IOUtils.toByteArray(is_dbicon.getByteStream()));
            //projectIcon = new ImageIcon(IOUtils.toByteArray(is_projectIcon.getByteStream()));
        } catch (IOException ex) {
            Logger.getLogger(CellRenderer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {

        super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);

        if (!(value instanceof PFCTreeNode)) {
            return this;
        }

        //value;
        setIcon(iconMap.get(value.getClass().getSimpleName()));
        
        /*
         if (value.getClass() == DBTreeNode.class) {
            //System.out.println("Modificando Icono de DBTreeNode " + dbIcon.getImageLoadStatus());
            setIcon(dbIcon);
            return this;
        }

        if (value.getClass() == ProyectoTreeNode.class) {
            //System.out.println("Modificando Icono de DBTreeNode " + dbIcon.getImageLoadStatus());
            setIcon(projectIcon);
            return this;
        }
*/


        return this;
    }
}
